﻿
using System;
using System.Collections.Generic;
using ValidationFramework.Reflection;

namespace ValidationFramework
{
   public class ValidationManagerFactory
    {
       private const string nullTargetErrorMessage = "target cannot be null. MemberValidationManager will be testing this reference. If you want to validate a static type or delay the assignment of the reference to test use the 'Validation' facade. Manager's are intended for instnace validation.";

	   private TypePolicyRepository _typePolicyRepository;
	   private MethodPolicyRepository _methodPolicyRepository;

	   /// <summary>
	   /// Initializes a new instance of the ValidationManagerFactory class.
	   /// </summary>
	   /// <param name="typePolicyCache"></param>
	   /// <param name="methodPolicyCache"></param>
	   public ValidationManagerFactory(TypePolicyRepository typePolicyCache, MethodPolicyRepository methodPolicyCache)
	   {
		   _typePolicyRepository = typePolicyCache;
		   _methodPolicyRepository = methodPolicyCache;
	   }

	   public MemberValidationManager GetPropertyValidationManager(object target)
       {
           if (target == null)
               throw new ArgumentNullException(nullTargetErrorMessage);

		   var typeKey = new TypeKey(target.GetType().TypeHandle);
			return new MemberValidationManager(target, _typePolicyRepository.GetPropertyPoliciesForType(typeKey));
       }

	   public MemberValidationManager GetFieldValidationManager(object target)
	   {
           if (target == null)
               throw new ArgumentNullException(nullTargetErrorMessage);

		   var typeKey = new TypeKey(target.GetType().TypeHandle);
		   return new MemberValidationManager(target, _typePolicyRepository.GetFieldPoliciesForType(typeKey));
	   }
    }
}
